package com.sayice.gomoku.dao.impl;

import com.sayice.gomoku.dao.BoardDao;
import com.sayice.gomoku.pojo.Board;
import com.sayice.gomoku.pojo.BoardInfo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 棋盘数据库操作
 */
public class BoardDaoImpl extends BaseDao implements BoardDao {
    @Override
    public int add(Board board) {
        String sql = "insert into board(name, price, uploaderId, modifierId) values(?, ?, ?, ?)";
        return update(null, sql, board.getName(), board.getPrice(), board.getUploaderId(), board.getModifierId());
    }
    
    @Override
    public int updateName(int id, String name) {
        String sql = "update board set name = ? where id = ?";
        return update(null, sql, name, id);
    }
    
    @Override
    public int updatePrice(int id, BigDecimal price) {
        String sql = "update board set price = ? where id = ?";
        return update(null, sql, price, id);
    }
    
    @Override
    public int updateModifier(int id, int modifierId) {
        String sql = "update board set modifierId = ? where id = ?";
        return update(null, sql, modifierId, id);
    }
    
    @Override
    public int addUseCount(int id, int count) {
        String sql = "update board set useCount = useCount + ? where id = ?";
        return update(null, sql, count, id);
    }
    
    @Override
    public int addBuyCount(int id) {
        String sql = "update board set buyCount = buyCount + 1 where id = ?";
        return update(null, sql, id);
    }
    
    @Override
    public Board queryById(int id) {
        String sql = "select * from board where id = ?";
        return queryForOne(Board.class, sql, id);
    }
    
    @Override
    public Board queryByName(String name) {
        String sql = "select * from board where name = ?";
        return queryForOne(Board.class, sql, name);
    }
    
    @Override
    public List<Board> queryByUserId(int userId) {
        String sql = "select * from board where "
                + "exists(select * from buy where userId = ? and boardId = board.id)";
        return queryForList(Board.class, sql, userId);
    }
    
    @Override
    public List<Board> queryAll() {
        String sql = "select * from board where not id = 1";
        return queryForList(Board.class, sql);
    }
    
    @Override
    public List<BoardInfo> queryAllInfo() {
        String sql = "select * from boardView";
        return queryForList(BoardInfo.class, sql);
    }
}
